package cn.jishiyu11.xeqb.base

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.utils.NetWorkUtils
import cn.jishiyu11.xeqb.utils.showShortToast

/**
 * 类功能介绍
 *
 * @Title: BaseFragment.java
 * @Description: TODO Fragment 基类初始化 layout布局
 */
abstract class BaseFragment : Fragment {//implements OnClickRefreshListener {

    private var mRootView: View? = null

    protected var onClickNum = 0

    private var time: Long = 0

    private var mContext: Activity? = null

    /**
     * 初始化Fragment的Layout
     *
     * @return
     */
    protected abstract val layout: Int

    /**
     * 判断是否是第一次看到这个页面 @Title: isFirstTime @author: xusonghui @Description:
     * TODO(这里用一句话描述这个方法的作用) @param: @return @return: boolean @throws
     */
    protected val isFirstTime: Boolean
        get() = onClickNum <= 1

    constructor() : super() {}


    constructor(activity: Activity) : super() {
        this.mContext = activity
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context as Activity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (savedInstanceState != null) {
            val FRAGMENTS_TAG = "Android:support:fragments"
            savedInstanceState.remove(FRAGMENTS_TAG)

        }
        mRootView = inflater.inflate(layout, container, false)
//        initView()
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    fun getmRootView(): View? {
        return mRootView
    }

    /**
     * 初始化View
     */
    protected abstract fun initView()

    /**
     * 通过ID获取相应的View
     *
     * @param id
     * @return
     */
    protected fun findViewById(id: Int): View? {
        return if (mRootView == null) {
            null
        } else mRootView!!.findViewById(id)
    }

    /**
     * 在MainActivity里面会在点击的时候去调用该方法，用于在加载的时候加载数据，建议在这里面加载数据 @Title:
     * loadData @author: xusonghui @Description:
     * TODO(这里用一句话描述这个方法的作用) @param: @return: void @throws
     */
    fun loadData() {
        onClickNum++
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) {
            loadData()
        } else {

        }
    }

    override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) {
        if (System.currentTimeMillis() - time < 1000) {
            return
        }
        time = System.currentTimeMillis()
        if (!NetWorkUtils.isNetworkConnected(activity)) {
            activity.showShortToast(getString(R.string.no_network_timed))
            return
        }
        super.startActivityForResult(intent, requestCode, options)
    }

    override fun startActivity(intent: Intent, options: Bundle?) {
        if (System.currentTimeMillis() - time < 1000) {
            return
        }
        time = System.currentTimeMillis()
        if (!NetWorkUtils.isNetworkConnected(activity)) {
            activity.showShortToast(getString(R.string.no_network_timed))
            return
        }
        super.startActivity(intent, options)
    }
}
